package com.wectrls.controller;

import com.wectrls.common.CommonVO;
import com.wectrls.entity.WecsUser;
import com.wectrls.service.WecsUserService;
import com.wectrls.vo.postvo.WecsUserAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.validation.constraints.Email;
import java.util.List;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-10-30 20:44
 */
@RestController
@RequestMapping(value = "/user")
public class WeCtrlsController {

    @Autowired
    private WecsUserService wecsUserServiceImpl;

    //todo 用户相关接口

    @PostMapping(value = "/register")
    public Object registerNewUser(@RequestBody WecsUserAddVo wecsUserAddVo, BindingResult bindingResult) {
        WecsUser newUser = wecsUserServiceImpl.addOne(wecsUserAddVo);
        //保存
        return new CommonVO<>(newUser);
    }

    @GetMapping(value = "/info")
    @RolesAllowed({"ROLE_SUPER_ADMIN", "ROLE_GROUP_ADMIN", "ROLE_GROUP_USER"})
    public Object getUserInfo() {
        WecsUser wecsUser = wecsUserServiceImpl.getUserInfo();
        return new CommonVO<>(wecsUser);
    }

    @GetMapping(value = "/check/email/uni")
    public Object checkEmailUnique(@Validated @Email(message = "邮箱格式不正确") String email) {
        List<WecsUser> wecsUsers = wecsUserServiceImpl.checkEmail(email);
        Boolean b = false;
        if (wecsUsers.size() > 0) {
            b = true;
        }
        return new CommonVO<>(b);
    }

    @GetMapping(value = "/find/list/{groupId}")
    public Object getUserListByGroupId(@PathVariable String groupId) {
        List<WecsUser> wecsUsers = wecsUserServiceImpl.findByGroupId(groupId);
        return new CommonVO<>(wecsUsers);
    }

}
